/*
nodejs nmp require
-express
-querystring
*/

var express = require('express');
GLOBAL.App = express();
GLOBAL.App.use(express.bodyParser());

var http = require('http');
var querystring = require('querystring');
App.all('/start', function(request, response){
	var post_data = querystring.stringify({
		status: 'start'
	});
	var pr = http.request({
		host: 'www.paintleb.com',
		post: 80,
		path: '/map3/oparation.php',
		method: 'POST',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'Content-Length': post_data.length,
		}
	},function(res){
		res.setEncoding('utf8');
		res.on('data', function (chunk) {
			var dataA = chunk.split('|');
			var dataC = new Array();
			for(i=1;i<dataA.length;i++){
				dataB = dataA[i].split(String.fromCharCode(28));
				position = dataB[2].split(',');
				var xy = dataB[2].split(',');
				var x = xy[0].replace(/\s+/g, ' ');
				var y = xy[1].replace(/\s+/g, ' ');
				dataC.push({
					images: 'https://graph.facebook.com/'+dataB[3]+'/picture',
					time: dataB[5],
					ulink: dataB[4],
					uname: dataB[3],
					detail: dataB[1],
					xy: dataB[2],
					x: x,
					y: y,
					id: dataB[6],
				});
			}
			response.send(JSON.stringify(dataC));
			response.end();
		});
	});
	pr.write(post_data);
	pr.end();
});
App.listen(3002);
console.log('listen port 3002');